home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: <75151.03563@compuserve.com>
- Newsgroups: comp.lang.c++
- Subject: How to ... declare a virtual static function in a class ?
- Date: 10 Jan 1996 08:10:44 GMT
- Organization: CompuServe Incorporated
- Message-ID: <4cvsa4$7g9@dub-news-svc-3.compuserve.com>
- NNTP-Posting-Host: dd74-136.compuserve.com
- Content-Type: text/plain
- Content-length: 3036
- X-Newsreader: AIR Mosaic (16-bit) version 1.00.198.07
-
-
- Jean-Pierre Schnyder <jschnyde@worldcom.ch> writes:
- >Hi,
- >
- >I guess this is not possible. Any idea on the rationale about this
- >limitation ? Thanks, Jean-Pierre
- >--
-
- Static virtual functions are not possible because of the way the virtual
- function mechanism works in C++, and the nature of static member
- functions.
-
- Non-static member functions all have an implied parameter passed
- to them i.e. <this>. The this pointer gives the member function access
- to the data of a specific instance of an object of the class.
-
- Static member functions do not the implied <this> pointer passed to them.
- They are, in effect, global functions with funny names. The static
- function member has no way of accessing the non-static data of the
- class, unless you explicitly give it access to an object.
-
- so given the class
-
- class A
- {
- public:
- int nonStaticFunc();
- static int staticFunc();
-
- private:
- int m_a;
- };
-
- int A::nonStaticFunc()
- {
- return m_a;
- }
-
- is valid, what the really amounts to is
-
- int A::nonStaticFunc( <this> )
- {
- return <this>->m_a;
- }
-
- where <this> is the implied pointer.
-
- int A::staticFunc()
- {
- return m_a;
- }
-
- is not valid. The static function gets no <this> pointer, so the compiler
- has no way of knowing in which instance of class A to look for the
- variable m_a.
-
- All this leads, slowly and painfully up to why static virtual functions are
- not possible.
-
- In a class that has virtual member functions, the compiler stores a
- table of function pointers in each instance of the class. When the
- object is intialized, the compiler puts a pointer to the appropriate function
- in the virtual table. This is how polymorphism works. When a virtual
- function is called, the compiler looks at the appropriate slot in the virtual
- table for the function to call.
-
- class A
- {
- ...
- virtual int virtFunc();
- static int statFunc() { return 1; }
- ...
- };
-
- int A::virtFunc()
- {
- return 1;
- }
-
- class B
- {
- ...
- virtual int virtFunc();
- static int statFunc() { return 10; }
- ...
- };
-
- int B::virtFunc()
- {
- return 2;
- }
-
- void func()
- {
- A * pa= new A; // virtual table points to A::virtFunc()
- B * pb= new B; // virtual table points to B::virtFunc()
- A * pab= new A; // virtual table points to B::virtFunc()
-
- pa->virtFunc(); // calls A::virtFunc()
- pb->virtFunc(); // calls B::virtFunc()
- pba->virtFunc(); // calls B::virtFunc()
-
- A::statFunc(); // calls A::statFunc()
- B::statFunc(): // calls B::statFunc()
-
- // pa->statFunc() - is illegal
- // pb->statFunc() - is illegal
- }
-
- Which leads to why virtual static functions don't work. A virtual
- function must be called from an object with an initialized virtual
- function table. A static member is never associated with an object
- instance, but is rather associated with a class. Since it is not
- associated with an object, there is never a virtual table to put
- a pointer to the function into.
-
- Hope this helps,
- Tom Keane
- 75151,03563@compuserve.com
-